package com.haierCamera.customapplication.utils;

import android.content.Context;
import android.content.SharedPreferences;


public class SharedPreferencesUtils {

    private static final String SP_NAME = "haier";
    private static SharedPreferences sp;

    public static void init(Context context) {
        SharedPreferencesUtils.sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
    }

    public static SharedPreferences instance() {
        return sp;
    }

    public static void saveInt(String key, int value) {
        sp.edit().putInt(key, value).apply();
    }

    public static boolean saveIntSync(String key, int value) {
        return sp.edit().putInt(key, value).commit();
    }

    public static long getLong(String key, long defaultValue) {
        return sp.getLong(key, defaultValue);
    }

    public static void saveLong(String key, long value) {
        sp.edit().putLong(key, value).apply();
    }

    public static boolean saveLongSync(String key, long value) {
        return sp.edit().putLong(key, value).commit();
    }

    public static int getInt(String key, int defaultValue) {
        return sp.getInt(key, defaultValue);
    }

    public static void saveString(String key, String value) {
        sp.edit().putString(key, value).apply();
    }

    public static boolean saveStringSync(String key, String value) {
        return sp.edit().putString(key, value).commit();
    }

    public static String getString(String key, String defaultValue) {
        return sp.getString(key, defaultValue);
    }

    public static void saveBoolean(String key, boolean value) {
        sp.edit().putBoolean(key, value).apply();
    }

    public static boolean saveBooleanSync(String key, boolean value) {
        return sp.edit().putBoolean(key, value).commit();
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        return sp.getBoolean(key, defaultValue);
    }

    public static void remove(String key) {
        sp.edit().remove(key).apply();
    }

    public static boolean removeSync(String key) {
        return sp.edit().remove(key).commit();
    }


    /**
     * 此方法已弃用，请使用 {@link #saveBoolean(String, boolean)}
     */
    @Deprecated
    public static void saveBoolean(Context context, String key, boolean value) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().putBoolean(key, value).commit();

    }

    /**
     * 此方法已弃用，请使用 {@link #getBoolean(String, boolean)}
     */
    @Deprecated
    public static boolean getBoolean(Context context, String key, boolean defValue) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        return sp.getBoolean(key, defValue);
    }

    /**
     * 此方法已弃用，请使用 {@link #saveString(String, String)}
     */
    @Deprecated
    public static void saveString(Context context, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().putString(key, value).commit();
    }

    /**
     * 此方法已弃用，请使用 {@link #getString(String, String)}
     */
    @Deprecated
    public static String getString(Context context, String key, String defValue) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getString(key, defValue);
    }

    /**
     * 此方法已弃用，请使用 {@link #saveInt(String, int)}
     */
    @Deprecated
    public static void saveInt(Context context, String key, int value) {
        sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().putInt(key, value).commit();
    }


    /**
     * 此方法已弃用，请使用 {@link #getInt(String, int)}
     */
    @Deprecated
    public static int getInt(Context context, String key, String value) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        return sp.getInt(key, 0);
    }

    /**
     * 此方法已弃用，请使用 {@link #saveLong(String, long)}
     */
    @Deprecated
    public static void saveLong(Context context, String key, Long value) {
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().putLong(key, value).commit();
    }

    /**
     * 此方法已弃用，请使用 {@link #getLong(String, long)}
     */
    @Deprecated
    public static long getLong(Context context, String key, long defValue) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sp.getLong(key, defValue);
    }


}
